﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HPAGMRestAPIWrapper;

namespace AGMRestAPITest
{
    /// <summary>
    /// Summary description for AGMTestTest
    /// </summary>
    [TestClass]
    public class AGMRunTest
    {
        public AGMRunTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext _testContextInstance;

        private static AGMConnection _connection;
        private static AGMRuns _runs;

        private AGMTestFolder _testFolder;
        private AGMTest _test1;
        private AGMTest _test2;
        private AGMTestSet _testSet;
        private AGMTestSetFolder _testSetFolder;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext)
        {
            _connection = new AGMConnection(AGMSetting.SERVERNAME, AGMSetting.USERNAME, AGMSetting.PASSWORD);
            _runs = new AGMRuns(_connection);
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void MyClassCleanup()
        {
            if (_connection != null)
                _connection.Logoff();
        }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize]
        public void MyTestInitialize()
        {
            #region Add test folder and 2 tests
            var testfolders = new AGMTestFolders(_connection);
            var rootTf = testfolders.Get(AGMSetting.ROOT_TESTFOLDER_ID);
            _testFolder = rootTf.CreateFolder("Test Folder");

            _test1 = _testFolder.AddTest("New Test 1");
            _test2 = _testFolder.AddTest("New Test 2"); 
            #endregion

            #region add test set folder and test set and add above 2 tests.
            var testsetfolders = new AGMTestSetFolders(_connection);
            var rootTsf = testsetfolders.Get(AGMSetting.ROOT_TESTSETFOLDER_ID);
            _testSetFolder = rootTsf.CreateFolder("Test Set Folder");
            _testSet = _testSetFolder.AddTestSet("New Test Set");

            _testSet.AddTests(new List<AGMTest> { _test1, _test2 });
            var testInstances = _testSet.GetTestInstances();
            foreach (var ti in testInstances)
            {
                Assert.IsTrue((ti.GetField("test-id").Value.Equals(_test1.Id.Value.ToString()) || ti.GetField("test-id").Value.Equals(_test2.Id.Value.ToString())));
            }            
            #endregion
        }
        //
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            _test1.Delete();
            _test2.Delete();
            _testFolder.Delete();
            _testSet.Delete();
            _testSetFolder.Delete();           

            System.IO.File.Delete(@"C:\Test.txt");
        }
        //
        #endregion

        [TestMethod]
        public void Run_AddUpdateDelete()
        {
            _testSet.RunTests();

            var lastruns = _testSet.GetTestLastRuns();

            foreach (var lastrun in lastruns)
            {
                lastrun.SetUpdateField("status", "Passed");
                lastrun.Update();

                var allruns = new AGMRuns(_connection);
                var myrun = allruns.Get(lastrun.Id.Value);

                Assert.AreEqual(myrun.GetField("status").Value, "Passed", "Update Run status fail.");

                lastrun.Delete();
            }
        }

        [TestMethod]
        public void Run_UploadDownLoadDeleteAttachment()
        {
            _testSet.RunTests();

            var lastruns = _testSet.GetTestLastRuns();

            foreach (var lastrun in lastruns)
            {
                lastrun.UploadAttachment(@"Test.txt");
                Assert.IsTrue(lastrun.IsAttachmentExists("Test.txt"), "Uploaded attachement is not exists.");

                lastrun.DownloadAttachment(@"Test.txt", @"c:\Test.txt");
                Assert.IsTrue(Helper.CheckAttachment(@"C:\Test.txt"), "Downloaded attachement is not exists.");

                lastrun.DeleteAttachment(@"Test.txt");
                Assert.IsFalse(lastrun.IsAttachmentExists("Test.txt"), "Uploaded attachement is not exists.");

                lastrun.Delete();
            }
        }
    }
}
